home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 248_01 / mdict.c < prev    next >
Text File  |  1989-08-16  |  3KB  |  125 lines

  1. /*    MDICT:    Main Dictionary processing for MicroSPELL 1.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dstruct.h"
  11. #include    "ddef.h"
  12. #include    "dsfx.h"
  13.  
  14. #if    CMPRS
  15. /* compression function globals */
  16. char lastword[NSTRING];        /* last word from dictionary */
  17. #endif
  18.  
  19. mopen()        /* open the main dicionary */
  20.  
  21. {
  22.     /* if it is already open, close it down */
  23.     if (mdptr != NULL)
  24.         fclose(mdptr);
  25.  
  26.     /* open op the main dictionary... */
  27.     if ((mdptr = popen(mdfile)) == NULL) {
  28.         printf("%%Can not find main dictionary\n");
  29.         return(FALSE);
  30.     }
  31.  
  32.     return(TRUE);
  33. }
  34.  
  35. mclose()    /* close the dictionary down */
  36.  
  37. {
  38.     /* if it is already open, close it down */
  39.     if (mdptr != NULL)
  40.         fclose(mdptr);
  41.     mdptr = NULL;
  42. }
  43.  
  44. char *nxtmword()    /* get the next word from the main dictionary */
  45.  
  46. {
  47.     static char word[NSTRING];    /* word to return */
  48. #if    CMPRS
  49.     char *gcword();
  50. #endif
  51.  
  52.     /* is it already closed? */
  53.     if (mdptr == NULL)
  54.         return(hivalue);
  55.  
  56.     /* get the next word */
  57. #if    CMPRS
  58.     if (gcword(word) == NULL) {
  59. #else
  60.     if (fgets(word, NSTRING - 1, mdptr) == NULL) {
  61. #endif
  62.         /* no more left!!!! close out */
  63.         fclose(mdptr);
  64.         mdptr = NULL;
  65.         return(hivalue);
  66.     }
  67.  
  68.     /* all's well, dump the return and return the word */
  69. #if    CMPRS == 0
  70.     word[strlen(word) - 1] = 0;
  71. #endif
  72.     return(word);
  73. }
  74.  
  75. #if    CMPRS
  76. char *gcword(word)    /* get the next compressed word */
  77.  
  78. char *word;    /* buffer to place word in */
  79.  
  80. {
  81.     register int c;        /* current character from file */
  82.     register char *wptr;    /* ptr into word to return */
  83.     register char *lptr;    /* pointer int last word */
  84.     int same;        /* # of common characters from last word */
  85.     int suffix;        /* index of common suffix */
  86.     int fgetc();
  87.  
  88.     /* first grab the #same count */
  89.     same = fgetc(mdptr);
  90.     if (same == EOF)    /* at EOF... return NULL */
  91.         return(NULL);
  92.     same -= 'A';
  93.  
  94.     /* and copy things across */
  95.     wptr = word;
  96.     lptr = lastword;
  97.     while (same-- > 0)
  98.         *wptr++ = *lptr++;
  99.  
  100.     c = fgetc(mdptr);
  101. #if     EBCDIC
  102.     /* opposite high bit flags */
  103.         while (c > 128) {
  104. #else
  105.         while (c < 128) {
  106. #endif
  107.         *wptr++ = c;
  108.         c = fgetc(mdptr);
  109.     }
  110.  
  111.     *wptr = 0;
  112.  
  113.     /* calculate the suffix to add... */
  114.     suffix = c & 127;
  115.     if (suffix != NSUFFIX)
  116.         strcpy(wptr, sfx[suffix]);
  117.  
  118.     /* save the current uncompressed word */
  119.     strcpy(lastword, word);
  120.  
  121.     /* and return the word */
  122.     return(word);
  123. }
  124. #endif
  125.